package me.zhengjie.pm.workerlist.service.impl;

import me.zhengjie.pm.workerlist.domain.EmployeeCenterDepartView;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.pm.workerlist.repository.EmployeeCenterDepartViewRepository;
import me.zhengjie.pm.workerlist.service.EmployeeCenterDepartViewService;
import me.zhengjie.pm.workerlist.service.dto.EmployeeCenterDepartViewDto;
import me.zhengjie.pm.workerlist.service.dto.EmployeeCenterDepartViewQueryCriteria;
import me.zhengjie.pm.workerlist.service.mapper.EmployeeCenterDepartViewMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yuxj
* @date 2020-04-15
*/
@Service
//@CacheConfig(cacheNames = "employeeCenterDepartView")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EmployeeCenterDepartViewServiceImpl implements EmployeeCenterDepartViewService {

    private final EmployeeCenterDepartViewRepository employeeCenterDepartViewRepository;

    private final EmployeeCenterDepartViewMapper employeeCenterDepartViewMapper;

    public EmployeeCenterDepartViewServiceImpl(EmployeeCenterDepartViewRepository employeeCenterDepartViewRepository, EmployeeCenterDepartViewMapper employeeCenterDepartViewMapper) {
        this.employeeCenterDepartViewRepository = employeeCenterDepartViewRepository;
        this.employeeCenterDepartViewMapper = employeeCenterDepartViewMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(EmployeeCenterDepartViewQueryCriteria criteria, Pageable pageable){
        Page<EmployeeCenterDepartView> page = employeeCenterDepartViewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(employeeCenterDepartViewMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<EmployeeCenterDepartViewDto> queryAll(EmployeeCenterDepartViewQueryCriteria criteria){
        return employeeCenterDepartViewMapper.toDto(employeeCenterDepartViewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public EmployeeCenterDepartViewDto findById(Integer id) {
        EmployeeCenterDepartView employeeCenterDepartView = employeeCenterDepartViewRepository.findById(id).orElseGet(EmployeeCenterDepartView::new);
        ValidationUtil.isNull(employeeCenterDepartView.getId(),"EmployeeCenterDepartView","id",id);
        return employeeCenterDepartViewMapper.toDto(employeeCenterDepartView);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public EmployeeCenterDepartViewDto create(EmployeeCenterDepartView resources) {
        return employeeCenterDepartViewMapper.toDto(employeeCenterDepartViewRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(EmployeeCenterDepartView resources) {
        EmployeeCenterDepartView employeeCenterDepartView = employeeCenterDepartViewRepository.findById(resources.getId()).orElseGet(EmployeeCenterDepartView::new);
        ValidationUtil.isNull( employeeCenterDepartView.getId(),"EmployeeCenterDepartView","id",resources.getId());
        employeeCenterDepartView.copy(resources);
        employeeCenterDepartViewRepository.save(employeeCenterDepartView);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            employeeCenterDepartViewRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EmployeeCenterDepartViewDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EmployeeCenterDepartViewDto employeeCenterDepartView : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" showName",  employeeCenterDepartView.getShowName());
            map.put(" empName",  employeeCenterDepartView.getEmpName());
            map.put(" centerName",  employeeCenterDepartView.getCenterName());
            map.put(" departmentName",  employeeCenterDepartView.getDepartmentName());
            map.put(" centerId",  employeeCenterDepartView.getCenterId());
            map.put(" departmentId",  employeeCenterDepartView.getDepartmentId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}